using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using ITS.Core.Domain.Messaging;
using ITS.Core.Domain.PageContents;
using ITS.Core.Domain.Common;
using ITS.Core.Domain.News;
using ITS.Core.Domain.Projects;
using System.Data.Entity.Infrastructure;
using ITS.Core.Domain.Members;
using ITS.Core.Domain.Events;
using ITS.Core.Domain.Publications;
using ITS.Core.Domain.Partners;
using ITS.Core.Domain.Users;
using ITS.Core.Domain.Contents;

namespace ITS.DAL.Concrete.EF
{
    public class EnterpriseContext : DbContext
    {
        public DbSet<SupportedLanguage> SupportedLanguages { get; set; }
        public DbSet<BusinessIdSeq> BusinessIdSeqs { get; set; }

        /*Users*/
        public DbSet<User> Users { get; set; }
        public DbSet<UserExtended> UserExtendeds { get; set; }
        public DbSet<Role> Roles { get; set; }

        /*Contents*/
        public DbSet<ContentCategory> ContentCategories { get; set; }
        public DbSet<ContentSubCategory> ContentSubCategories { get; set; }
        public DbSet<ContentSubCategoryDescription> ContentSubCategoryDescriptions { get; set; }
        public DbSet<ContentItem> ContentItems { get; set; }
        public DbSet<ContentItemDescription> ContentItemDescriptions { get; set; }

        /*News*/
        public DbSet<NewsCategory> NewsCategories { get; set; }
        public DbSet<NewsItem> NewsItems { get; set; }
        public DbSet<NewsCategoryDescription> NewsCategoryDescriptions { get; set; }
        public DbSet<NewsTag> NewsTags { get; set; }

        /*Page contents*/
        public DbSet<PageContentCategory> PageContentCategories { get; set; }
        public DbSet<PageContent> PageContents { get; set; }
        public DbSet<PageContentDescription> PageContentDescriptions { get; set; }
        public DbSet<PageContentImage> PageContentImages { get; set; }

        /*Projects*/
        public DbSet<ProjectCategory> ProjectCategories { get; set; }
        public DbSet<Project> Projects { get; set; }
        public DbSet<ProjectDescription> ProjectDescriptions { get; set; }
        public DbSet<ProjectImage> ProjectImages { get; set; }

        /*Members*/
        public DbSet<Member> Members { get; set; }
        public DbSet<MemberCategory> MemberCategories { get; set; }

        /*Events*/
        public DbSet<Event> Events { get; set; }
        public DbSet<EventCategory> EventCategories { get; set; }

        /*Publications*/
        public DbSet<Publication> Publications { get; set; }
        public DbSet<PublicationCategory> PublicationCategories { get; set; }
        /*Partners*/
        public DbSet<Partner> Partners { get; set; }
        public DbSet<PartnerCategory> PartnerCategories { get; set; }
        /*Orthers*/
        public DbSet<EmailAccount> EmailAccounts { get; set; }
        public DbSet<Setting> Settings { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            /*Many to many relationships*/
            modelBuilder.Entity<NewsItem>().
            HasMany(c => c.NewsTags).
            WithMany(p => p.NewsItems).
            Map(
            m =>
            {
                m.MapLeftKey("NewsItemID");
                m.MapRightKey("TagID");
                m.ToTable("NewsTagNewsItem");
            });

            modelBuilder.Entity<NewsItem>().
            HasMany(c => c.NewsCategories).
            WithMany(p => p.NewsItems).
            Map(
            m =>
            {
                m.MapLeftKey("NewsItemID");
                m.MapRightKey("CategoryID");
                m.ToTable("NewsCategoryMapping");
            });

            modelBuilder.Entity<User>().
            HasMany(c => c.Roles).
            WithMany(p => p.Users).
            Map(
            m =>
            {
                m.MapLeftKey("UserID");
                m.MapRightKey("RoleID");
                m.ToTable("UserRole");
            });
        }
    }
}
